/*
 *Program Name: Jet.cpp
 *Written By:   Cheng-Hsiu Hsieh
 */

#include "Jet.h"

#include "SharedConstants.h"

#include "corona/corona.h"

#include <GL/glut.h>
#include <math.h>

//-----------------------------------------------------------------------------
// Globals:
//-----------------------------------------------------------------------------
cJet gJet;

//-----------------------------------------------------------------------------
// Functions:
//-----------------------------------------------------------------------------
cJet* GetJet()
{
	return &gJet;
}

//-----------------------------------------------------------------------------
// class cJet:
//-----------------------------------------------------------------------------
cJet::cJet():cDrawable( 4.0f )
{
  mHealth = 1;
  mScore = 0;
  mCURRENT_BULLET = 0;
  for ( int i = 0 ; i < 20 ; i++ )
  {
    mRBullet[i] = new cBullet();
    mLBullet[i] = new cBullet();
  }
}

cJet::cJet( int iHealth, int iScore , int iDamage ):cDrawable( 4.0f )
{
  mHealth = iHealth;
  mScore = iScore;
  for ( int i = 0 ; i < 20 ; i++ )
  {
    mRBullet[i] = new cBullet( iDamage );
    mLBullet[i] = new cBullet( iDamage );
  }
}

cJet::~cJet()
{
}

bool cJet::isDie( int iDamage )
{
  return ( iDamage > mHealth );    
}

void cJet::setHealth( int iNew )
{
  mHealth = iNew;
}

void cJet::setScore( int iScore )
{
  mScore = iScore;
}

void cJet::addScore( int iScore )
{
  mScore += iScore;
}

int cJet::getHealth()
{
  return mHealth;
}

int cJet::getScore()
{
  return mScore;
}


cBullet* cJet::getRBullet( int i )
{
  return mRBullet[i];
}

cBullet* cJet::getLBullet( int i )
{
  return mLBullet[i];
}


void cJet::Frame( float fRed, float fGreen , float fBlue )
{
	// body
  /// below window
 
  glBegin(GL_TRIANGLES);
  glColor3f( fRed, fGreen, fBlue );
  glVertex3f(1.5f, 1.0f, 0.75f);
  glVertex3f(1.5f, 0.5f, 1.0f); 
  glVertex3f(4.0f, 0.5f, 0.0f); 
  
  glVertex3f(1.5f, 1.0f, -0.75f);  
  glVertex3f(4.0f, 0.5f, 0.0f);
  glVertex3f(1.5f, 0.5f, -1.0f); 

  /// front top
  glVertex3f(4.0f, 0.5f, 0.0f); 
  glVertex3f(3.0f, 1.0f, 0.0f);
  glVertex3f(1.5f, 1.0f, 0.75f);

  glVertex3f(4.0f, 0.5f, 0.0f);   
  glVertex3f(1.5f, 1.0f, -0.75f);
  glVertex3f(3.0f, 1.0f, 0.0f);

  /// front bottom
  glVertex3f(0.0f, -0.5f, 0.0f);
  glVertex3f(4.0f, 0.5f, 0.0f);  
  glVertex3f(1.5f, 0.5f, 1.0f); 
  
  glVertex3f(4.0f, 0.5f, 0.0f); 
  glVertex3f(0.0f, -0.5f, 0.0f); 
  glVertex3f(1.5f, 0.5f, -1.0f); 

  /// little triangle 
  glVertex3f(0.0f, 0.75f, 1.0f);
  glVertex3f(0.0f, 0.25f, 1.0f);
  glVertex3f(1.5f, 0.5f, 1.0f);

  glVertex3f(0.0f, 0.25f, -1.0f);
  glVertex3f(0.0f, 0.75f, -1.0f);
  glVertex3f(1.5f, 0.5f, -1.0f);

  /// triangle under little triangle
  glVertex3f(0.0f, 0.25f, 1.0f);
  glVertex3f(0.0f, -0.5f, 0.0f); 
  glVertex3f(1.5f, 0.5f, 1.0f);

  glVertex3f(0.0f, -0.5f, 0.0f); 
  glVertex3f(0.0f, 0.25f, -1.0f);
  glVertex3f(1.5f, 0.5f, -1.0f);

  /// wing front
  glVertex3f(0.0f, 0.75f, 1.0f);
  glVertex3f(-2.0f, 0.5f, 4.0f);
  glVertex3f(0.0f, 0.25f, 1.0f);

  glVertex3f(-2.0f, 0.5f, -4.0f);
  glVertex3f(0.0f, 0.75f, -1.0f);
  glVertex3f(0.0f, 0.25f, -1.0f);

  /// wing back
  glVertex3f(-3.5f, 0.25f, 1.0f);
  glVertex3f(-3.0f, 0.5f, 4.0f);
  glVertex3f(-3.5f, 0.75f, 1.0f);
  
  glVertex3f(-3.0f, 0.5f, -4.0f);
  glVertex3f(-3.5f, 0.25f, -1.0f);
  glVertex3f(-3.5f, 0.75f, -1.0f);

  /// tail wing
  glVertex3f(-2.0f, 1.5f, 0.0f);
  glVertex3f(-3.5f, 3.0f, 0.0f);
  glVertex3f(-3.5f, 1.0f, 0.25f);
  
  glVertex3f(-3.5f, 3.0f, 0.0f);
  glVertex3f(-2.0f, 1.5f, 0.0f);
  glVertex3f(-3.5f, 1.0f, -0.25f);

  //// back side  
  glVertex3f(-3.5f, 3.0f, 0.0f);
  glVertex3f(-3.5f, 1.0f, -0.25f);
  glVertex3f(-3.5f, 1.0f, 0.25f);

  /// tail side
  glVertex3f(-3.5f, 0.75f, 1.0f);
  glVertex3f(-4.0f, 0.5f, 0.5f);
  glVertex3f(-3.5f, 0.25f, 1.0f);

  glVertex3f(-4.0f, 0.5f, -0.5f);
  glVertex3f(-3.5f, 0.75f, -1.0f);
  glVertex3f(-3.5f, 0.25f, -1.0f);

  glEnd();

  glBegin(GL_QUADS);
  /// on top little triangle
  glVertex3f(-0.5f, 1.5f, 0.0f);
  glVertex3f(0.0f, 0.75f, 1.0f);
  glVertex3f(1.5f, 0.5f, 1.0f);
  glVertex3f(1.5f, 1.0f, 0.75f);
  
  glVertex3f(0.0f, 0.75f, -1.0f);
  glVertex3f(-0.5f, 1.5f, 0.0f);
  glVertex3f(1.5f, 1.0f, -0.75f);
  glVertex3f(1.5f, 0.5f, -1.0f);

  /// wing top  
  glVertex3f(-3.5f, 0.75f, 1.0f);
  glVertex3f(-3.0f, 0.5f, 4.0f);
  glVertex3f(-2.0f, 0.5f, 4.0f);
  glVertex3f(0.0f, 0.75f, 1.0f);
  
  glVertex3f(-3.0f, 0.5f, -4.0f);
  glVertex3f(-3.5f, 0.75f, -1.0f);
  glVertex3f(0.0f, 0.75f, -1.0f);
  glVertex3f(-2.0f, 0.5f, -4.0f);

  /// wing bommon  
  glVertex3f(-3.0f, 0.5f, 4.0f);
  glVertex3f(-3.5f, 0.25f, 1.0f);
  glVertex3f(0.0f, 0.25f, 1.0f);
  glVertex3f(-2.0f, 0.5f, 4.0f);
  
  glVertex3f(-3.5f, 0.25f, -1.0f);
  glVertex3f(-3.0f, 0.5f, -4.0f);
  glVertex3f(-2.0f, 0.5f, -4.0f);
  glVertex3f(0.0f, 0.25f, -1.0f);

  /// under wing
  glVertex3f(-3.5f, 0.25f, 1.0f);
  glVertex3f(-3.5f, -0.5f, 0.0f);
  glVertex3f(0.0f, -0.5f, 0.0f);
  glVertex3f(0.0f, 0.25f, 1.0f);
  
  glVertex3f(-3.5f, -0.5f, 0.0f);
  glVertex3f(-3.5f, 0.25f, -1.0f);
  glVertex3f(0.0f, 0.25f, -1.0f);
  glVertex3f(0.0f, -0.5f, 0.0f);

  /// body top
  glVertex3f(-0.5f, 1.5f, 0.0f);
  glVertex3f(-3.5f, 1.5f, 0.0f);
  glVertex3f(-3.5f, 0.75f, 1.0f);
  glVertex3f(0.0f, 0.75f, 1.0f);

  glVertex3f(-3.5f, 1.5f, 0.0f);
  glVertex3f(-0.5f, 1.5f, 0.0f);
  glVertex3f(0.0f, 0.75f, -1.0f);
  glVertex3f(-3.5f, 0.75f, -1.0f);

  /// rear
  //// outside view
  ///// top
  glVertex3f(-3.5f, 1.5f, 0.0f);
  glVertex3f(-4.0f, 1.0f, 0.0f);
  glVertex3f(-4.0f, 0.5f, 0.5f);
  glVertex3f(-3.5f, 0.75f, 1.0f);

  glVertex3f(-4.0f, 1.0f, 0.0f);
  glVertex3f(-3.5f, 1.5f, 0.0f);
  glVertex3f(-3.5f, 0.75f, -1.0f);
  glVertex3f(-4.0f, 0.5f, -0.5f);

  ///// bottom
  glVertex3f(-4.0f, 0.5f, 0.5f);
  glVertex3f(-4.0f, 0.0f, 0.0f);
  glVertex3f(-3.5f, -0.5f, 0.0f);
  glVertex3f(-3.5f, 0.25f, 1.0f);

  glVertex3f(-4.0f, 0.0f, 0.0f);
  glVertex3f(-4.0f, 0.5f, -0.5f);
  glVertex3f(-3.5f, 0.25f, -1.0f);
  glVertex3f(-3.5f, -0.5f, 0.0f);

  //// inside view
  ///// top
  glVertex3f(-4.0f, 1.0f, 0.0f);
  glVertex3f(-3.75f, 1.0f, 0.0f);
  glVertex3f(-3.75f, 0.5f, 0.5f);
  glVertex3f(-4.0f, 0.5f, 0.5f);

  glVertex3f(-3.75f, 1.0f, 0.0f);
  glVertex3f(-4.0f, 1.0f, 0.0f);
  glVertex3f(-4.0f, 0.5f, -0.5f);
  glVertex3f(-3.75f, 0.5f, -0.5f);

  ///// bottom
  glVertex3f(-4.0f, 0.0f, 0.0f);
  glVertex3f(-4.0f, 0.5f, 0.5f);
  glVertex3f(-3.75f, 0.5f, 0.5f);
  glVertex3f(-3.75f, 0.0f, 0.0f);

  glVertex3f(-4.0f, 0.5f, -0.5f);
  glVertex3f(-4.0f, 0.0f, 0.0f);
  glVertex3f(-3.75f, 0.0f, 0.0f);
  glVertex3f(-3.75f, 0.5f, -0.5f);

  /// gun
  ////Right gun
  /////Right
  glVertex3f(0.0f, 0.6f, 2.0f );
  glVertex3f(-1.0f, 0.6f, 2.0f );
  glVertex3f(-1.0f, 0.4f, 2.0f );
  glVertex3f(0.0f, 0.4f, 2.0f );
  /////Left
  glVertex3f(-1.0f, 0.6f, 1.8f );
  glVertex3f(0.0f, 0.6f, 1.8f );
  glVertex3f(0.0f, 0.4f, 1.8f );
  glVertex3f(-1.0f, 0.4f, 1.8f );
  /////Top
  glVertex3f(0.0f, 0.6f, 1.8f );
  glVertex3f(-1.0f, 0.6f, 1.8f );
  glVertex3f(-1.0f, 0.6f, 2.0f );
  glVertex3f(0.0f, 0.6f, 2.0f );
  /////Bottom
  glVertex3f(-1.0f, 0.4f, 1.8f );
  glVertex3f(0.0f, 0.4f, 1.8f );
  glVertex3f(0.0f, 0.4f, 2.0f );
  glVertex3f(-1.0f, 0.4f, 2.0f );
  /////Front
  /////////This is also the initial location for bullets
  glVertex3f(0.0f, 0.6f, 2.0f );
  glVertex3f(0.0f, 0.6f, 1.8f );
  glVertex3f(0.0f, 0.4f, 1.8f );
  glVertex3f(0.0f, 0.4f, 2.0f );

  ////Left gun
  /////Right
  glVertex3f(-1.0f, 0.6f, -2.0f );
  glVertex3f(0.0f, 0.6f, -2.0f );
  glVertex3f(0.0f, 0.4f, -2.0f );
  glVertex3f(-1.0f, 0.4f, -2.0f );
  /////Left
  glVertex3f(0.0f, 0.6f, -1.8f );
  glVertex3f(-1.0f, 0.6f, -1.8f );
  glVertex3f(-1.0f, 0.4f, -1.8f );
  glVertex3f(0.0f, 0.4f, -1.8f );
  /////Top
  glVertex3f(-1.0f, 0.6f, -1.8f );
  glVertex3f(0.0f, 0.6f, -1.8f );
  glVertex3f(0.0f, 0.6f, -2.0f );
  glVertex3f(-1.0f, 0.6f, -2.0f );
  /////Bottom
  glVertex3f(0.0f, 0.4f, -1.8f );
  glVertex3f(-1.0f, 0.4f, -1.8f );
  glVertex3f(-1.0f, 0.4f, -2.0f );
  glVertex3f(0.0f, 0.4f, -2.0f );
  /////Front
  /////////This is also the initial location for bullets
  glVertex3f(0.0f, 0.6f, -1.8f );
  glVertex3f(0.0f, 0.6f, -2.0f );
  glVertex3f(0.0f, 0.4f, -2.0f );
  glVertex3f(0.0f, 0.4f, -1.8f );


  //// flame
  glColor3f(1.0f, 0.0f, 0.0f );  
  glVertex3f(-3.75f, 1.0f, 0.0f);
  glVertex3f(-3.75f, 0.5f, -0.5f);
  glVertex3f(-3.75f, 0.0f, 0.0f);
  glVertex3f(-3.75f, 0.5f, 0.5f);
  glEnd();

  // windshield
  glBegin(GL_TRIANGLE_FAN);
  glColor3f(0.2f, 0.2f, 0.2f);
  glVertex3f(1.5f, 1.75f, 0.0f);
  glVertex3f(1.5f, 1.0f, 0.75f);
  glVertex3f(3.0f, 1.0f, 0.0f);
  glVertex3f(1.5f, 1.0f, -0.75f);
  glVertex3f(0.5f, 2.0f, 0.0f);
  glVertex3f(1.5f, 1.0f, 0.75f);
  glEnd();

  glBegin(GL_TRIANGLES);
  glVertex3f(1.5f, 1.0f, 0.75f);
  glVertex3f(0.5f, 2.0f, 0.0f);
  glVertex3f(-0.5f, 1.5f, 0.0f);

  glVertex3f(-0.5f, 1.5f, 0.0f);
  glVertex3f(0.5f, 2.0f, 0.0f);
  glVertex3f(1.5f, 1.0f, -0.75f);
  glEnd();
  
}

void cJet::fire( float  mZVelocity )
{
  if ( mRBullet[ mCURRENT_BULLET ]->reset( getXPosition() + 1.9f , 
                                          getYPosition() + 0.5F, 
                                          getZPosition() ) )
    mRBullet[ mCURRENT_BULLET ]->fire( mZVelocity );

  if ( mLBullet[ mCURRENT_BULLET ]->reset( getXPosition() - 1.9f , 
                                          getYPosition() + 0.5F, 
                                          getZPosition() ) )
    mLBullet[ mCURRENT_BULLET ]->fire( mZVelocity );

  if ( mCURRENT_BULLET < 19 )
    mCURRENT_BULLET++;
  else
    mCURRENT_BULLET = 0;
 
  ////Left 
  //Bullet.setXPosition( getXPosition() - 1.9f );
  //Bullet.setYPosition( getYPosition() + 0.5F);
  //Bullet.setZPosition( getZPosition() );

  ////Right
  //Bullet.setXPosition( getXPosition() + 1.9f );
  //Bullet.setYPosition( getYPosition() + 0.5F);
  //Bullet.setZPosition( getZPosition() );

  //Bullet.fire( mZVelocity );
}

